<template>
    <el-dialog
            :title="!dataForm.billId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                          
                <el-form-item label="客户ID" prop="customerId">
                        <el-input v-model.number="dataForm.customerId" placeholder="客户ID"></el-input>
                </el-form-item>
                <el-form-item label="账单日期" prop="billDate">
                        <el-date-picker
                                v-model="dataForm.billDate"
                                type="datetime"
                                placeholder="账单日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="账单总金额" prop="totalAmount">
                        <el-input v-model="dataForm.totalAmount" placeholder="账单总金额"></el-input>
                </el-form-item>
                <el-form-item label="账单状态" prop="billStatus">
                    <el-select v-model="dataForm.billStatus" placeholder="请选择账单状态">
                        <el-option label="未发送" value="未发送"></el-option>
                        <el-option label="已发送" value="已发送"></el-option>
                    </el-select>
                </el-form-item> 
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            billId: null,
            customerId: null,
            billDate: null,
            totalAmount: null,
            billStatus: null
          },
          dataRule: {
            billId: [
                        { required: true, message: '账单ID，主键不能为空', trigger: 'blur' }
            ],
            customerId: [
                        { required: true, message: '客户ID，外键关联发货人表或收货人表不能为空', trigger: 'blur' }
            ],
            billDate: [
                        { required: true, message: '账单日期不能为空', trigger: 'blur' }
            ],
            totalAmount: [
                        { required: true, message: '账单总金额不能为空', trigger: 'blur' }
            ],
            billStatus: [
                        { required: true, message: '账单状态（如：未发送、已发送）不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.billId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.billId = id || 0
            if (this.dataForm.billId) {
              this.$http({
                url: this.$http.adornUrl(`/app/customer_bills/info/${this.dataForm.billId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appcustomerbills
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/customer_bills/${!this.dataForm.billId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>